#region Using
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using CY.GFive.Core.Business;
using CY.Utility.DBUtility;
#endregion Using

namespace CY.GFive.DALProviders.SqlServerProvider
{
	public class StdCredentialsProvider : CY.GFive.Core.Providers.DALProvider.IStdCredentialsProvider
	{
		#region Sql
		private static readonly string SqlSelectStdCredentials = "SELECT [Id], [StdCode],[ProjectCode],[CredentialCode],[CredentialCate],[IsTaken],[IsMaken] FROM [StdCredentials] WHERE  [Id]=@Id";

		private static readonly string SqlInsertStdCredentials = "INSERT INTO [StdCredentials]([StdCode],[ProjectCode],[CredentialCode],[CredentialCate],[IsTaken],[IsMaken]) values(@StdCode,@ProjectCode,@CredentialCode,@CredentialCate,@IsTaken,@IsMaken);SELECT SCOPE_IDENTITY();";

		private static readonly string SqlUpdateStdCredentials = "UPDATE [StdCredentials] SET [StdCode] = @StdCode,[ProjectCode] = @ProjectCode,[CredentialCode] = @CredentialCode,[CredentialCate] = @CredentialCate,[IsTaken] = @IsTaken,[IsMaken] = @IsMaken WHERE  [Id]=@Id";

		private static readonly string SqlDeleteStdCredentials = "DELETE [StdCredentials] WHERE  [Id]=@Id";

		private static readonly string SqlGetAllStdCredentials = "SELECT [Id], [StdCode],[ProjectCode],[CredentialCode],[CredentialCate],[IsTaken],[IsMaken] FROM [StdCredentials]";
		#endregion

		#region IStdCredentialsProvider Members
		public Core.Business.StdCredentials Select(int id)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, id);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlSelectStdCredentials);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				Core.Business.StdCredentials stdCredentials = new Core.Business.StdCredentials();

				if (!reader.IsDBNull(0)) stdCredentials.Id = reader.GetInt32(0);
				if (!reader.IsDBNull(1)) stdCredentials.StdCode = reader.GetString(1);
				if (!reader.IsDBNull(2)) stdCredentials.ProjectCode = reader.GetString(2);
				if (!reader.IsDBNull(3)) stdCredentials.CredentialCode = reader.GetString(3);
				if (!reader.IsDBNull(4)) stdCredentials.CredentialCate = reader.GetString(4);
				if (!reader.IsDBNull(5)) stdCredentials.IsTaken = reader.GetBoolean(5);
				if (!reader.IsDBNull(6)) stdCredentials.IsMaken = reader.GetBoolean(6);

				return stdCredentials;
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();

				return null;
			}
		}

		public void Insert(Core.Business.StdCredentials stdCredentials)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, stdCredentials.StdCode);
			sql.AddParameter("@ProjectCode", SqlDbType.NVarChar, stdCredentials.ProjectCode);
			sql.AddParameter("@CredentialCode", SqlDbType.NVarChar, stdCredentials.CredentialCode);
			sql.AddParameter("@CredentialCate", SqlDbType.NVarChar, stdCredentials.CredentialCate);
			sql.AddParameter("@IsTaken", SqlDbType.Bit, stdCredentials.IsTaken);
			sql.AddParameter("@IsMaken", SqlDbType.Bit, stdCredentials.IsMaken);
			SqlDataReader reader = sql.ExecuteSqlReader(SqlInsertStdCredentials);

			if (reader != null && !reader.IsClosed && reader.Read())
			{
				if (!reader.IsDBNull(0)) stdCredentials.Id = int.Parse(reader.GetDecimal(0).ToString());

				reader.Close();
			}
			else
			{
				if (reader != null && !reader.IsClosed)
					reader.Close();
			}
		}

		public void Update(Core.Business.StdCredentials stdCredentials)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@StdCode", SqlDbType.NVarChar, stdCredentials.StdCode);
			sql.AddParameter("@ProjectCode", SqlDbType.NVarChar, stdCredentials.ProjectCode);
			sql.AddParameter("@CredentialCode", SqlDbType.NVarChar, stdCredentials.CredentialCode);
			sql.AddParameter("@CredentialCate", SqlDbType.NVarChar, stdCredentials.CredentialCate);
			sql.AddParameter("@IsTaken", SqlDbType.Bit, stdCredentials.IsTaken);
			sql.AddParameter("@IsMaken", SqlDbType.Bit, stdCredentials.IsMaken);
			sql.AddParameter("@Id", SqlDbType.Int, stdCredentials.Id);
			sql.ExecuteSql(SqlUpdateStdCredentials);
		}

		public void Delete(Core.Business.StdCredentials stdCredentials)
		{
			SqlServerUtility sql = new SqlServerUtility();

			sql.AddParameter("@Id", SqlDbType.Int, stdCredentials.Id);
			sql.ExecuteSql(SqlDeleteStdCredentials);
		}

		public IList<Core.Business.StdCredentials> GetAllStdCredentials()
		{
			IList<Core.Business.StdCredentials> stdCredentialslist = new List<Core.Business.StdCredentials>();
			SqlServerUtility sql = new SqlServerUtility();

			SqlDataReader reader = sql.ExecuteSqlReader(SqlGetAllStdCredentials);

			if(reader != null)
			{
				while(reader.Read())
				{
					Core.Business.StdCredentials stdCredentials = new Core.Business.StdCredentials();

					if (!reader.IsDBNull(0)) stdCredentials.Id = reader.GetInt32(0);
					if (!reader.IsDBNull(1)) stdCredentials.StdCode = reader.GetString(1);
					if (!reader.IsDBNull(2)) stdCredentials.ProjectCode = reader.GetString(2);
					if (!reader.IsDBNull(3)) stdCredentials.CredentialCode = reader.GetString(3);
					if (!reader.IsDBNull(4)) stdCredentials.CredentialCate = reader.GetString(4);
					if (!reader.IsDBNull(5)) stdCredentials.IsTaken = reader.GetBoolean(5);
					if (!reader.IsDBNull(6)) stdCredentials.IsMaken = reader.GetBoolean(6);

					stdCredentials.MarkOld();
					stdCredentialslist.Add(stdCredentials);
				}
				reader.Close();
			}
			return stdCredentialslist;
		}
		#endregion
	}
}
